package com.bstek.pms.tools.utils;

import com.bstek.pms.tools.model.DBProperties;

import java.sql.*;

/**
 * @Description 数据原连接类
 * @Author hans
 * @CreateDate 2020-7-5
 */
public class DbUtils {

    public static Connection getConnection(DBProperties dbProperties) throws Exception {

        Class.forName(dbProperties.getDbDriver());
        Connection conn = DriverManager.getConnection(dbProperties.getDbUrl(), dbProperties.getDbUserName(), dbProperties.getDbPwd());

        return conn;
    }

    /**
     * 关闭连接的方法
     *
     * @param conn
     */
    public static void closeConn(Connection conn) throws SQLException {
        if (conn != null) conn.close();

    }

    /**
     * 关闭PS的方法
     *
     * @param ps
     */
    public static void closePs(PreparedStatement ps) throws SQLException {
        if (ps != null) ps.close();
    }

    /**
     * 关闭RS的方法
     *
     * @param rs
     */
    public static void closeRs(ResultSet rs) throws SQLException {
        if (rs != null) rs.close();

    }

    /**
     * 关闭这三个连接
     *
     * @param rs
     * @param ps
     * @param conn
     */
    public static void closeAll(ResultSet rs, PreparedStatement ps,
                                Connection conn) throws SQLException {
        closeRs(rs);
        closePs(ps);
        closeConn(conn);
    }
}
